const WebProcessModel = require('./webProcessModel')
const WebAjaxModel = require('./webAjaxModel')
const WebErrorModel = require('./webErrorModel')
const ServerProcessModel = require('./serverProcessModel')
const ServerErrorModel = require('./serverErrorModel')
const Base = require('../base')

/**
 * 用户Mongomo模型
 */
class LogMongo extends Base{

  constructor(){
    super()
  }

  /**
   * 查找列表数据
   * @param mongoModel  mongo模型对象
   * @param data 查询参数
   */
  static _find(mongoModel, data) {
    const {page, count, sort, startTime, endTime, selKey, conditions} = data
    const params = {
      date: {
        $gte: new Date(startTime),
        $lte: new Date(endTime)
      }
    }
    if (selKey && conditions) {
      params[selKey] = { $regex: conditions,"$options":"i"}
    }
    let field = {}
    const skip = (page - 1) * count
    const model = mongoModel.find(params, field).skip(skip).limit(count)
    model.sort({'date': sort ? -1 : 1})
    return model.exec()
  }

  /**
   * 删除数据
   * @param mongoModel mongo模型对象
   * @param id 删除ID
   */
  static _delete(mongoModel, id) {
    return mongoModel.deleteOne({ _id: this.objId(id) })
  }

  /**
   * 删除所有
   * @param mongoModel mongo模型对象
   */
  static _deleteAll(mongoModel) {
    return mongoModel.remove()
  }

  /**
   * 添加前端流程日志
   */
  static addWebProcess(data) {
    return WebProcessModel(data).save()
  }

  /**
   * 查询前端流程日志
   */
  static webProcessList(data) {
    return this._find(WebProcessModel, data)
  }

  /**
   * 删除前端流程日志
   */
  static webProcessRemoveOne(id) {
    return this._delete(WebProcessModel, id)
  }

  /**
   * 删除前端流程日志
   */
  static webProcessRemoveAll() {
    return this._deleteAll(WebProcessModel)
  }

  /**
   * 添加前端Ajax日志
   */
  static addWebAjax(data) {
    return WebAjaxModel(data).save()
  }

  /**
   * 查询前端Ajax日志
   */
  static webAjaxList(data) {
    return this._find(WebAjaxModel, data)
  }

  /**
   * 删除前端Ajax日志
   */
  static webAjaxRemoveOne(id) {
    return this._delete(WebAjaxModel, id)
  }

  /**
   * 删除前端Ajax日志
   */
  static webAjaxRemoveAll() {
    return this._deleteAll(WebAjaxModel)
  }

  /**
   * 添加前端Error日志
   */
  static addWebError(data) {
    return WebErrorModel(data).save()
  }

  /**
   * 查询前端Ajax日志
   */
  static webErrorList(data) {
    return this._find(WebErrorModel, data)
  }

  /**
   * 删除前端Ajax日志
   */
  static webErrorRemoveOne(id) {
    return this._delete(WebErrorModel, id)
  }

  /**
   * 删除前端Ajax日志
   */
  static webErrorRemoveAll() {
    return this._deleteAll(WebErrorModel)
  }

  /**
   * 添加服务器流程日志
   */
  static addServerProcess(data) {
    return ServerProcessModel(data).save()
  }

  /**
   * 查询服务器流程日志
   */
  static serverProcessList(data) {
    return this._find(ServerProcessModel, data)
  }

  /**
   * 删除服务器流程日志
   */
  static serverProcessRemoveOne(id) {
    return this._delete(ServerProcessModel, id)
  }

  /**
   * 删除服务器流程日志
   */
  static serverProcessRemoveAll() {
    return this._deleteAll(ServerProcessModel)
  }

  /**
   * 添加服务器Error日志
   */
  static addServerError(data) {
    ServerErrorModel(data).save().then((data) => {
      console.log('addServerError = ', data)
    }).catch(() => {

    })
  }

  /**
   * 查询服务器Error日志
   */
  static serverErrorList(data) {
    return this._find(ServerErrorModel, data)
  }

  /**
   * 删除服务器Error日志
   */
  static serverErrorRemoveOne(id) {
    return this._delete(ServerErrorModel, id)
  }

  /**
   * 删除服务器Error日志
   */
  static serverErrorRemoveAll() {
    return this._deleteAll(ServerErrorModel)
  }
}

module.exports = LogMongo
